import pymongo
from copy import copy
import redis as pyredis

from libs.mysqldb import MySQLdb
from configs.configs import *

# 无密码连接
mongo_client = pymongo.MongoClient(mongodb_config['host'], int(mongodb_config['port']))

# 存放知乎用户Cookies
zhihu_cookies_db = mongo_client['cookies']['zhihu']
# 代理IP日志
proxy_db = mongo_client['ips']['proxies']

# 通过连接池的方式去Redis
ipredis = pyredis.StrictRedis(
    connection_pool=pyredis.ConnectionPool(
        host=redis_config['host'],
        port=int(redis_config['port']),
        db=1,
        encoding='utf8',
        # 设置为True，让redis帮我们完成解码操作
        decode_responses=True
    )
)

init_mysql_config = copy(mysql_config)
init_mysql_config.pop('database')
init_db = MySQLdb(init_mysql_config)

sql_db = None


def init_sql_db():
    global sql_db
    sql_db = MySQLdb(mysql_config)


init_sql_db()

def get_sql_db():
    return sql_db